﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SystemLYH.Infrstructre
{
    public class BaseRepository<T> : IRepository<T> where T : class, new()
    {
        protected readonly MyDbContext _dbContext;
        /// <summary>
        /// 上下午构造函数
        /// </summary>
        /// <param name="dbContext"></param>
        public BaseRepository(MyDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> CreaseAsync(T entity)
        {
            await _dbContext.AddAsync(entity);
            return await _dbContext.SaveChangesAsync();
        }
        //
        //public async Task DeleteAsync(int id)
        //{
        //    var list=await _dbContext.Set<T>().FindAsync(id);
        //    _dbContext.Remove(list);
        //    return await _dbContext.SaveChangesAsync();
        //}
        /// <summary>
        ///  查询列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetAllAsync()
        {
            return await _dbContext.Set<T>().ToListAsync();
        }
        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetAsync(int id)
        {
            return await _dbContext.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
            _dbContext.Update(entity);
            return await _dbContext.SaveChangesAsync();
        }
    }
}
